package com.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;

import com.example.john.myfund.MainActivity;
import com.httputils.CacheBean;

public class DialogUtils {

	public static final int NO_WAIT_DIALOG = 0;// 不需要弹等待对话框
	public static final int WAIT_DIALOG = 1;// 普通android自带对话框
	public static final int WAIT_DIALOG_CANNOTCLOSE = 2;// back键不能取消loading

	@SuppressWarnings("deprecation")
	public static ProgressDialog showProgressDialog(Context context, int waitDialogType, String title,
			String message) {
		ProgressDialog dialog = null;
		switch (waitDialogType) {
		case NO_WAIT_DIALOG:
			dialog = null;
			break;
		case WAIT_DIALOG:
			dialog = new ProgressDialog(context);
			dialog.getWindow().setBackgroundDrawable(new BitmapDrawable());
			dialog.setTitle(title);
			dialog.setMessage(message);
			dialog.show();
			dialog.setCanceledOnTouchOutside(false);
			break;
		case WAIT_DIALOG_CANNOTCLOSE:
			dialog = new ProgressDialog(context);
			dialog.getWindow().setBackgroundDrawable(new BitmapDrawable());
			dialog.setTitle(title);
			dialog.setMessage(message);
			dialog.show();
			dialog.setCancelable(false);
			dialog.setCanceledOnTouchOutside(false);
			break;
		default:
			break;
		}
		return dialog;
	}

	/**
	 * 显示消息的dialog
	 * 
	 * @param context 上下文
	 * @param message 显示的消息
	 */
	public static void showMessageSetDialog(final Context context, String message) {
		Builder builder = new Builder(context);
		builder.setMessage(message);
		builder.setPositiveButton("确定", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.setCancelable(false).show();
	}

	/**
	 * 网络设置工具方法
	 * 
	 * @param context 上下文对象
	 */
	public static void showNetworkSetDialog(final Context context) {
		Builder builder = new Builder(context);
		builder.setTitle("提示");
		builder.setMessage("当前网络不可用，是否设置网络？");
		builder.setPositiveButton("设置", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent("android.settings.WIFI_SETTINGS");
				context.startActivity(intent);
				dialog.cancel();
			}
		});
		if (!((Activity) context).isFinishing()) {
			builder.setCancelable(true).show();
		}
	}

	public static void showExitDialog(final Context context, OnClickListener listener) {
		Builder builder = new Builder(context);
		builder.setMessage("确认退出吗？");
		builder.setPositiveButton("退出", listener);
		builder.setNegativeButton("取消", null);
		builder.create().show();
	}

	public static void showDiyDialog(final Context context, String msg, OnClickListener listener) {
		Builder builder = new Builder(context);
		builder.setMessage(msg);
		builder.setPositiveButton("确定", listener);
		builder.create().show();
	}

	public static void showDiyDialogCancel(final Context context, String msg, String posBtn,
			OnClickListener listener) {
		Builder builder = new Builder(context);
		builder.setMessage(msg);
		builder.setPositiveButton(posBtn, listener);
		builder.setNegativeButton("取消", null);
		builder.create().show();
	}

	public static void showDiyDialogCancel(final Context context, String msg, OnClickListener listener) {
		Builder builder = new Builder(context);
		builder.setMessage(msg);
		builder.setPositiveButton("确定", listener);
		builder.setNegativeButton("取消", null);
		builder.create().show();
	}

	/**
	 * 往外弹的警示框(点击退出)
	 * 
	 * @param message 内容
	 * @return AlertDialog对象 add by zhangxy
	 *         2013/01/25
	 */
	public static AlertDialog getDialogAndFinish(final Context context, String message) {
		Builder builder = new Builder(context);
		builder.setMessage(message);
		builder.setPositiveButton("确定", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				((Activity) context).finish();
			}
		});
		builder.setCancelable(false);
		return builder.show();
	}

	/**
	 * 弹出登录超时对话框，点击确认后跳转到主页面并退出登录
	 * 
	 * @param context 上下文对象
	 * @param message 显示信息
	 * @param cacheBean 缓存
	 * @return
	 */
	public static AlertDialog loginTimeOut(final Context context, String message, final CacheBean cacheBean) {
		Builder builder = new Builder(context);
		builder.setMessage(message);
		builder.setPositiveButton("确定", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				cacheBean.setTimeout(true);
				Intent intent = new Intent(context, MainActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(intent);
				((Activity) context).finish();
			}
		});
		builder.setCancelable(false);
		return builder.show();
	}

}
